﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_BadKarma : Buff
    {
        public Spell_BadKarma()
            : base()
        {
            minutesLeft = -2;
        }

        private int valuesRemoved = 0;

        public override void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            Character characterToDebuff = (Character)targetCreatureBase;
            SpellIcon iconToUse = _spellIcon.clone();
            if (characterToDebuff.hateManager.getKarmaCount() > 1)
            {
                if (iconToUse.Name.IndexOf(" Rank ") > -1)
                    iconToUse.Name = iconToUse.Name.Remove(iconToUse.Name.IndexOf(" Rank "));

                iconToUse.Name = iconToUse.Name + " Rank " + characterToDebuff.hateManager.getKarmaCount().ToString();
            }

            base.Resume(targetCreatureBase, iconToUse, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);
            
            valuesRemoved = System.Convert.ToInt32(Math.Ceiling(characterToDebuff.skillManager.getMaxSkill() / 2));

            int powerToRaiseTo = characterToDebuff.hateManager.getKarmaCount() - 1;
            if (powerToRaiseTo != 0)
                valuesRemoved = valuesRemoved * System.Convert.ToInt32(Math.Pow(2, powerToRaiseTo));

            TargetCreatureBase.MaxHPAdd = TargetCreatureBase.MaxHPAdd - (valuesRemoved * 5);
            TargetCreatureBase.MaxManaAdd = TargetCreatureBase.MaxHPAdd - (valuesRemoved * 5);
            TargetCreatureBase.MaxStaminaAdd = TargetCreatureBase.MaxHPAdd - (valuesRemoved * 5);

            if (TargetCreatureBase.HP > TargetCreatureBase.MaxHP)
                TargetCreatureBase.HP = TargetCreatureBase.MaxHP;
            if (TargetCreatureBase.Mana > TargetCreatureBase.MaxMana)
                TargetCreatureBase.Mana = TargetCreatureBase.MaxMana;
            if (TargetCreatureBase.Stamina > TargetCreatureBase.MaxStamina)
                TargetCreatureBase.Stamina = TargetCreatureBase.MaxStamina;

            TargetCreatureBase.Armor = TargetCreatureBase.Armor - (valuesRemoved * 2);
            TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire - (valuesRemoved * 2);
            TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce - (valuesRemoved * 2);
            TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly - (valuesRemoved * 2);
            TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy - (valuesRemoved * 2);
            TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic - (valuesRemoved * 2);

            TargetCreatureBase.StrengthAdd = TargetCreatureBase.StrengthAdd - valuesRemoved;
            TargetCreatureBase.DexterityAdd = TargetCreatureBase.DexterityAdd - valuesRemoved;
            TargetCreatureBase.AgilityAdd = TargetCreatureBase.AgilityAdd - valuesRemoved;
            TargetCreatureBase.WisdomAdd = TargetCreatureBase.WisdomAdd - valuesRemoved;
            TargetCreatureBase.IntelligenceAdd = TargetCreatureBase.IntelligenceAdd - valuesRemoved;

            TargetCreatureBase.Update_Character();
        }
        protected override void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (DateTime.Now > TargetCreatureBase.hateManager.getKarmaEarliestExpire())
                {
                    TargetCreatureBase.hateManager.removeKarmaEarliestExpire();
                    if (TargetCreatureBase.hateManager.getKarmaCount() == 0)
                        RemoveSpell(true, true);
                    else
                    {
                        RemoveSpell(false, false);
                        Resume(TargetCreatureBase, spellIcon, -1, -1, -1, -1, -1, -1, null);

                        TargetCreatureBase.buffManager.UpdateBuffs();
                        TargetCreatureBase.SendMessage("Your Bad Karma has reduced to rank " + TargetCreatureBase.hateManager.getKarmaCount().ToString() + "!", "server");
                    }
                }
                else
                    spellTimer.Start();
            }
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            lock (Managers.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance).LockObj)
            {
                TargetCreatureBase.MaxHPAdd = TargetCreatureBase.MaxHPAdd + (valuesRemoved * 5);
                TargetCreatureBase.MaxManaAdd = TargetCreatureBase.MaxHPAdd + (valuesRemoved * 5);
                TargetCreatureBase.MaxStaminaAdd = TargetCreatureBase.MaxHPAdd + (valuesRemoved * 5);

                TargetCreatureBase.Armor = TargetCreatureBase.Armor + (valuesRemoved * 2);
                TargetCreatureBase.ResistFire = TargetCreatureBase.ResistFire + (valuesRemoved * 2);
                TargetCreatureBase.ResistIce = TargetCreatureBase.ResistIce + (valuesRemoved * 2);
                TargetCreatureBase.ResistHoly = TargetCreatureBase.ResistHoly + (valuesRemoved * 2);
                TargetCreatureBase.ResistUnholy = TargetCreatureBase.ResistUnholy + (valuesRemoved * 2);
                TargetCreatureBase.ResistMagic = TargetCreatureBase.ResistMagic + (valuesRemoved * 2);

                TargetCreatureBase.StrengthAdd = TargetCreatureBase.StrengthAdd + valuesRemoved;
                TargetCreatureBase.DexterityAdd = TargetCreatureBase.DexterityAdd + valuesRemoved;
                TargetCreatureBase.AgilityAdd = TargetCreatureBase.AgilityAdd + valuesRemoved;
                TargetCreatureBase.WisdomAdd = TargetCreatureBase.WisdomAdd + valuesRemoved;
                TargetCreatureBase.IntelligenceAdd = TargetCreatureBase.IntelligenceAdd + valuesRemoved;
            }

            TargetCreatureBase.Update_Character();
        }
    }
}
